package com.rdxer.common.ex;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class StringEx {
    /**
     * 尾部  去除后缀
     *
     * @param string  字符串
     * @param suffixlist  后缀列表
     * @return  新字符串
     */
    public static String trimTrailing(String string, List<String> suffixlist) {

        for (int i = 0; i < suffixlist.size(); i++) {
            String suf = suffixlist.get(i);
            if (string.endsWith(suf)) {
                string = trimTrailing(string, suf);
                i = -1;
            }
        }

        return string;
    }

    /**
     * 尾部  去除后缀
     *
     * @param string 字符串
     * @param suffix 后缀
     * @return 新字符串
     */
    public static String trimTrailing(String string, String suffix) {
        if (string.endsWith(suffix)) {
            return string.substring(0, string.length() - suffix.length());
        }
        return string;
    }

    public static boolean isEmpty(String str) {
        return (str == null || "".equals(str));
    }

    public static boolean isBalnk(String str) {
        return !hasText(str);
    }

    public static boolean hasText(CharSequence str) {
        return (str != null && str.length() > 0 && containsText(str));
    }

    /**
     * 是否包含任意字符
     * @param str .
     * @return .
     */
    public static boolean containsText(CharSequence str) {
        int strLen = str.length();
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public static String block(String str, Integer keep) {
        return block(str, "*", null, keep);
    }

    public static String block(String str, String mask, Double percent, Integer keep) {

        if (isEmpty(str)) {
            return str;
        }

        int length = str.length();

        if (length == 1) {
            return mask;
        }

        double v;
        int vi = 1;
        if (percent != null) {
            v = length * percent;
            vi = (int) v;
        } else if (keep != null) {
            vi = keep;
        }

        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < length; i++) {
            String substring = str.substring(i, i + 1);
            if (i < vi) {
                stringBuilder.append(substring);
            } else {
                stringBuilder.append(mask);
            }
        }

        return stringBuilder.toString();
    }

    public static String block(String str) {
        return block(str, "*", 0.5, null);
    }

    public static String replace(String str, int start, int end, String newValue) {
        String startStr = str.substring(0, start);
        String endStr = str.substring(end);

        String s = startStr + newValue + endStr;

        return s;
    }

    public static boolean equals(String str1, String str2) {
        if (str1 != null) {
            return str1.equals(str2);
        }
        if (str2 == null) {
            return true;
        }
        return false;
    }


    // 获取扩展名
    public static String getExtension(String name) {
        String regex = "/";
        if (Objects.equals(File.separator, "\\")){
            regex = "\\\\";
        }
        String[] split = name.split(regex);
        if (split.length == 0){
            return null;
        }
        String lastComponent = split[split.length - 1];
        String[] split2 = lastComponent.split("\\.");
        if (split2.length == 0){
            return null;
        }
        return split2[split2.length-1];
    }



    public static String replaceBatch(String source, Map<String,String> kv) {
        for (Map.Entry<String, String> entry : kv.entrySet()) {
            source = source.replace(entry.getKey(), entry.getValue());
        }
        return source;
    }

    public static String replaceAllByRecurve(String content, String target, String newStr) {
        if (StringEx.isEmpty(content)){
            return content;
        }
        if (StringEx.isEmpty(target)){
            throw new RuntimeException("target is empty" + target);
        }
        if (newStr == null){
            throw new RuntimeException("newStr is null" + newStr);
        }

        return replaceAllByRecurveWork(content, target, newStr);
    }
    public static String replaceAllByRecurveWork(String content, String target, String newStr) {
        for (int i1 = 0; true ; ) {
            if (i1 >= content.length()){
                break;
            }
            boolean isOver = false;
            boolean eq = true;
            for (int i2 = 0; i2 < target.length(); i2++) {
                int c1Index = i1 + i2;
                if (c1Index >= content.length()){
                    isOver = true;
                    break;
                }
                char c1 = content.charAt(c1Index);
                char c2 = target.charAt(i2);
                if (c1 != c2) {
                    eq = false;
                    break;
                }
            }
            if (isOver) {
                break;
            }
            if (eq) {
                content = replace(content,i1,i1+target.length(),newStr);
            }else{
                i1++;
            }
        }
        return content;
    }

    public static String removeAllBlankLine(String content) {
        String[] split = content.split("\n");
        StringBuilder builder = new StringBuilder();
        for (String line : split) {
            if (!StringUtils.isBlank(line)){
                builder.append(line);
                builder.append("\n");
            }
        }
        return builder.toString();
    }

    public static List<String> pathComponentsByString(String path){
        int indexOf = path.indexOf("?");
        if (indexOf != -1){
            path = path.substring(0,indexOf);
        }
        String[] split = path.split("/");
        return Arrays.stream(split).collect(Collectors.toList());
    }
}
